package com.ushine.web.component.excel.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.ushine.web.component.excel.handler.MemberExcelDataHandler;
import com.ushine.web.component.excel.model.MemberDO;
import com.ushine.web.component.excel.model.OrderDO;
import com.ushine.web.component.excel.model.ProductDO;
import com.ushine.web.component.http.HttpResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: yls
 * @Date 2021/10/26 14:31
 * @Description: excel导入导出
 * @Version 1.0
 **/
@Slf4j
@RestController
@Api(tags = "EasyPoiController", value = "EasyPoi导入导出测试")
@RequestMapping("/excel")
public class EasyPoiController {

    @ApiOperation(value = "导出会员列表Excel")
    @GetMapping(value = "/exportMemberList")
    public void exportMemberList(HttpServletRequest request, HttpServletResponse response) {
        List<MemberDO> memberList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            MemberDO memberDO = new MemberDO();
            memberDO.setId(1L);
            memberDO.setBirthday(new Date());
            memberDO.setNickname("测试" + i);
            memberDO.setGender(i);
            memberDO.setPhone("1357895645" + i);
            memberDO.setUsername("用户" + i);
            memberList.add(memberDO);
        }
        ModelMap map = new ModelMap();
        ExportParams exportParams = new ExportParams("会员列表", "会员列表", ExcelType.XSSF);
        map.put(NormalExcelConstants.DATA_LIST, memberList);
        map.put(NormalExcelConstants.PARAMS, exportParams);
        map.put(NormalExcelConstants.FILE_NAME, "memberList");
        map.put(NormalExcelConstants.CLASS, MemberDO.class);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    @ApiOperation(value = "从Excel导入会员列表")
    @PostMapping(value = "/importMemberList")
    @ResponseBody
    public HttpResponseVO<Object> importMemberList(@RequestPart("file") MultipartFile file) {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        try {
            List<MemberDO> memberList = ExcelImportUtil.importExcel(file.getInputStream(), MemberDO.class, params);
            memberList.forEach(k -> log.info("导入会员列表：{}", k));
            return new HttpResponseVO<>().ok("导入会员列表成功：", memberList);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResponseVO<>().error("导入失败");
        }
    }

    @ApiOperation(value = "导出订单列表Excel")
    @GetMapping(value = "/exportOrderList")
    public void exportOrderList(HttpServletRequest request,
                                HttpServletResponse response) {
        ModelMap map = new ModelMap();
        List<OrderDO> orderList = getOrderList();
        ExportParams params = new ExportParams("订单列表", "订单列表", ExcelType.XSSF);
        //导出时排除一些字段
        params.setExclusions(new String[]{"ID", "出生日期", "性别"});
        map.put(NormalExcelConstants.DATA_LIST, orderList);
        map.put(NormalExcelConstants.CLASS, OrderDO.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME, "orderList");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    @ApiOperation(value = "自定义导出会员列表Excel")
    @RequestMapping(value = "/exportMemberListByCustom", method = RequestMethod.GET)
    public void exportMemberListByCustom(
                                         HttpServletRequest request,
                                         HttpServletResponse response) {
        ModelMap map = new ModelMap();
        List<MemberDO> memberList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            MemberDO memberDO = new MemberDO();
            memberDO.setId(1L);
            memberDO.setBirthday(new Date());
            memberDO.setNickname("测试" + i);
            memberDO.setGender(i);
            memberDO.setPhone("1357895645" + i);
            memberDO.setUsername("用户" + i);
            memberList.add(memberDO);
        }
        ExportParams params = new ExportParams("会员列表", "会员列表", ExcelType.XSSF);
        //对导出结果进行自定义处理
        MemberExcelDataHandler handler = new MemberExcelDataHandler();
        handler.setNeedHandlerFields(new String[]{"昵称"});
        params.setDataHandler(handler);
        map.put(NormalExcelConstants.DATA_LIST, memberList);
        map.put(NormalExcelConstants.CLASS, MemberDO.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME, "memberList");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }


    /**
     * 功能描述:
     * 〈 组装测试数据 〉
     *
     * @param
     * @return : java.util.List<com.fight.component.excel.Order>
     * @author : yls
     * @date : 2021/10/26 16:02
     */
    private List<OrderDO> getOrderList() {
        List<OrderDO> orderList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            OrderDO orderDO = new OrderDO();
            orderDO.setId((long) i);
            orderDO.setOrderSn("测试" + i);
            orderList.add(orderDO);
        }
        List<ProductDO> productList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            ProductDO productDO = new ProductDO();
            productDO.setBrandName("aa");
            productDO.setCount(i);
            productDO.setName("asd"+i);
            productDO.setPrice(new BigDecimal("12.56"));
            productDO.setSubTitle("测试");
            productList.add(productDO);
        }
        List<MemberDO> memberList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            MemberDO memberDO = new MemberDO();
            memberDO.setId(1L);
            memberDO.setBirthday(new Date());
            memberDO.setNickname("测试" + i);
            memberDO.setGender(i);
            memberDO.setPhone("1357895645" + i);
            memberDO.setUsername("用户" + i);
            memberList.add(memberDO);
        }
        for (int i = 0; i < orderList.size(); i++) {
            OrderDO order = orderList.get(i);
            order.setMember(memberList.get(i));
            order.setProductList(productList);
        }
        return orderList;
    }

}
